<?php
namespace app\mini\model;

use think\facade\Db;
use app\common\traits\ActionRecordTrait;
use think\Model;
use app\admin\controller\ApiCommon;

use app\crm\model\CustomerLossModel;
use app\crm\model\OrderStatistics;
use app\admin\model\ShopOrderModel;
use app\admin\model\CustomerModel;
use app\admin\model\ShopModel;
use app\shop\model\Refund as OrderModel;
use app\mini\model\MiniStisticsModel;
use app\crm\model\ReserveandLost;

use app\mini\model\CustomerPointGoodsBuy;



class ActivityCustomerMiniModel extends Model
{
     /**

     * @var string

     */

    protected $name = 'admin_sys';
    protected $defaultSoftDelete = 0;
    protected $autoWriteTimestamp = true;


    public function getCard($request): array
    {

        $customer_id = $request['customer_id'] ?? '';

        $service_id = $request['service_id'] ?? '';

        $service_name = $request['service_name'] ?? '';

        if(!$customer_id){

            return ['error' => '未获取到用户信息，请确认是否登录', 'code' => 401];

        }

        $biaoqianCode = '635';

        $where = [];

        array_push($where,['id','=',$customer_id]);

        array_push($where,['member_label','like','%'.$biaoqianCode.'%']);

        $customer_info = Db::name('customer')->where($where)->find();

        $where = [];

        array_push($where,['shop_id','=',$customer_info['shop_id']]);

        array_push($where,['position_id','=','7']);

        $shop_personId = Db::name('shop_person')->where($where)->value('id');

        if(!$customer_info){

            return ['error' => '暂未获取活动资格！', 'code' => 401];

        }

        $nurse_id = $customer_info['nurse_id'] > '0' ? $customer_info['nurse_id'] : $shop_personId;

        $where = [];

        array_push($where,['customer_id','=',$customer_id]);

        array_push($where,['name','=',$service_name]);

        array_push($where,['status','=',1]);

        $customer_service = Db::name('service')->where($where)->find();

        if($customer_service){

            return ['error' => '您已经领取了该卡，请勿重复领取', 'code' => 401];

        }

        $addData = [

            'name' => $service_name,

            'shop_id' => $customer_info['shop_id'],

            'customer_id' => $customer_id,

            'service_type' => '2',

            'num' => '1',

            'start_time' => time(),

            'end_time' => strtotime('+1 year'),

            'create_time' => time(),

            'kk_user_id' => $nurse_id,

            'goods_id' => $service_id,

        ];

        $id = Db::name('service')->insertGetId($addData);

        if($id){

            return ['error' => '领取成功！', 'code' => 401];

        }
        
    }

    public function isGetCard($request): array
    {

        return [
            'overlay_show' => true,
        ];
        $customer_id = $request['customer_id'] ?? '';

        $where = [];

        array_push($where,['id','=',$customer_id]);

        $shop_id = Db::name('customer')->where($where)->value('shop_id');

        $shop_info = Db::name('shop')->where('shop_id',$shop_id)->find();


        $over_time = strtotime('2024-12-31');

        // 是否填写问卷
        // $where = [];

        // array_push($where,['customer_id','=',$customer_id]);

        // array_push($where,['questionnaire_id','=','6']);

        // $ised = Db::name('question_customer')->where($where)->find();

        // $shop_info['market_id'] == '8';
        // echo "<pre>";
        // print_r($shop_info);
        // echo "</pre>";
        // exit;

        
        if($shop_info['type'] == '1' || $shop_info['type'] == '3'){

            $overlay_show = true;

            if(time() > $over_time){

                $overlay_show = false;
                
            }

            // 屏蔽川渝地区
            // if($shop_info['market_id'] == '23' || $shop_info['market_id'] == '24' || $shop_info['market_id'] == '12' || $shop_info['market_id'] == '25'){

            //     $overlay_show = false;

            // }

        }else{

            $overlay_show = false;

        }
        
        return [
            
            'overlay_show' => $overlay_show,
            
        ];

    }

    public function downPointGoods($request):array
    {
        $customer_id = $request['customer_id'] ?? '';

        $pointGoodsId = $request['point_goods_id'] ?? '28';

        $where = [];

        array_push($where,['customer_id','=',$customer_id]);

        array_push($where,['point_goods_id','=',$pointGoodsId]);

        $isGet = Db::name('customer_point_goods_buy')->where($where)->select()->toArray();

        if($isGet){

            return ['error' => '您已经领取了该卡，请勿重复领取', 'code' => 401];

        }

        Db::startTrans();
        try {
            $customerPointGoodsBuy = new CustomerPointGoodsBuy();
            $customerPointGoodsBuy->save([
                'point_goods_id'=>$pointGoodsId,
                'customer_id'=>$customer_id,
                'status'=>1,
                'start_time'=>strtotime('2024-03-08'),
                'end_time'=>strtotime('2024-03-11 23:59:59'),
            ]);
            CustomerPointModel::pointChange($customer_id,'42');
            Db::commit();
            return ['error' => '领取成功！', 'code' => 401];
        } catch (\Exception $e) {
            Db::rollback();
            return ['error' => '领取失败！', 'code' => 401];
        }
    }
 
    public function downServiceCard($request):array
    {
        $cardIds = [165,168];

        $customer_id = $request['customer_id'] ?? '';

        if(!$customer_id){

            return ['error' => '客户信息错误！', 'code' => 401];

        }

        $shop_id = Db::name('customer')->where('id',$customer_id)->value('shop_id');

        $goods_name = Db::name('shop_goods')->where([['id','in',$cardIds]])->column('name','id');

        $where = [];

        array_push($where,['goods_id','in',$cardIds]);
        array_push($where,['customer_id','=',$customer_id]);
        array_push($where,['order_id','=','']);

        $res = Db::name('service')->where($where)->select()->toArray();

        if($res){

            return ['error' => '您已经领取了该卡，请勿重复领取', 'code' => 401];

        }

        if($request['type'] == 1){
            $cardId = 168;
            $num = 2;
        }

        if($request['type'] == 2){
            $cardId = 165;
            $num = 1;
        }

        $addData = [
            'name' => $goods_name[$cardId],
            'shop_id' => $shop_id,
            'customer_id' => $customer_id,
            'service_type' => 2,
            'num' => $num,
            'status' => 1,
            'experience' => 1,
            'goods_id' => $cardId,
            'create_time' => time(),
        ];

        $id = Db::name('service')->insertGetId($addData);

        if($id){
            return ['error' => '领取成功！', 'code' => 401];
        }else{
            return ['error' => '领取失败！', 'code' => 401];
        }
        // echo "<pre>";
        // print_r($addData);
        // echo "</pre>";
        // exit;
    }
}
